package com.socialsoftware.ui.Discover;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.socialsoftware.model.Comment;
import com.socialsoftware.model.Moment;
import com.socialsoftware.model.User;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DiscoverViewModel extends ViewModel {

    private final MutableLiveData<List<Moment>> mMoments;
    private final MutableLiveData<String> mText;

    public DiscoverViewModel() {
        mText = new MutableLiveData<>();
        mText.setValue("这是朋友圈页面");
        
        mMoments = new MutableLiveData<>();
        loadMockData();
    }

    public LiveData<String> getText() {
        return mText;
    }
    
    public LiveData<List<Moment>> getMoments() {
        return mMoments;
    }
    
    public void refreshMoments() {
        // In a real app, this would fetch data from a repository
        loadMockData();
    }
    
    public void toggleLike(Moment moment, int position) {
        List<Moment> currentMoments = mMoments.getValue();
        if (currentMoments != null && position < currentMoments.size()) {
            Moment updatedMoment = currentMoments.get(position);
            
            // Toggle like status
            boolean isLiked = !updatedMoment.isLikedByMe();
            updatedMoment.setLikedByMe(isLiked);
            
            // Update like count
            int likeCount = updatedMoment.getLikeCount();
            updatedMoment.setLikeCount(isLiked ? likeCount + 1 : Math.max(0, likeCount - 1));
            
            // Update user in likes list
            List<User> likedUsers = new ArrayList<>(updatedMoment.getLikedUsers());
            User currentUser = new User("我", "");
            
            if (isLiked) {
                likedUsers.add(currentUser);
            } else {
                likedUsers.removeIf(user -> "我".equals(user.getName()));
            }
            
            updatedMoment.setLikedUsers(likedUsers);
            
            // Notify observers
            mMoments.setValue(currentMoments);
        }
    }
    
    public void addComment(Moment moment, int position, String commentText) {
        List<Moment> currentMoments = mMoments.getValue();
        if (currentMoments != null && position < currentMoments.size()) {
            Moment updatedMoment = currentMoments.get(position);
            
            // Create new comment
            Comment newComment = new Comment(
                    new User("我", ""),
                    commentText,
                    System.currentTimeMillis()
            );
            
            // Add to comments list
            List<Comment> comments = new ArrayList<>(updatedMoment.getComments());
            comments.add(newComment);
            updatedMoment.setComments(comments);
            
            // Update comment count
            updatedMoment.setCommentCount(comments.size());
            
            // Notify observers
            mMoments.setValue(currentMoments);
        }
    }
    
    private void loadMockData() {
        List<Moment> moments = new ArrayList<>();
        
        // Create mock users with IDs
        User user1 = new User("user_1", "张三", "张三", "https://randomuser.me/api/portraits/men/1.jpg");
        User user2 = new User("user_2", "李四", "李四", "https://randomuser.me/api/portraits/women/2.jpg");
        User user3 = new User("user_3", "王五", "王五", "https://randomuser.me/api/portraits/men/3.jpg");
        User user4 = new User("user_4", "赵六", "赵六", "https://randomuser.me/api/portraits/women/4.jpg");
        
        // Create mock comments
        List<Comment> comments1 = Arrays.asList(
                new Comment(user2, "真不错，在哪里拍的？", System.currentTimeMillis() - 3600000),
                new Comment(user1, "在北京郊区，风景很美", System.currentTimeMillis() - 1800000)
        );
        
        List<Comment> comments2 = Arrays.asList(
                new Comment(user3, "看起来很好吃！", System.currentTimeMillis() - 7200000),
                new Comment(user4, "下次带我去！", System.currentTimeMillis() - 3600000),
                new Comment(user2, "我也想尝尝", System.currentTimeMillis() - 1200000)
        );
        
        // Create mock liked users
        List<User> likedUsers1 = Arrays.asList(user2, user3, user4);
        List<User> likedUsers2 = Arrays.asList(user1, user3);
        List<User> likedUsers3 = Arrays.asList(user2, user4);
        
        // Create mock image URLs
        List<String> images1 = Arrays.asList(
                "https://picsum.photos/id/10/800/600",
                "https://picsum.photos/id/11/800/600",
                "https://picsum.photos/id/12/800/600"
        );
        
        List<String> images2 = Arrays.asList(
                "https://picsum.photos/id/20/800/600"
        );
        
        List<String> images3 = Arrays.asList(
                "https://picsum.photos/id/30/800/600",
                "https://picsum.photos/id/31/800/600",
                "https://picsum.photos/id/32/800/600",
                "https://picsum.photos/id/33/800/600"
        );
        
        // Create mock moments
        moments.add(new Moment(
                1,
                user1,
                "今天天气真好，出去走走拍了几张照片分享给大家！#旅行 #摄影",
                images1,
                System.currentTimeMillis() - 86400000,
                "北京市朝阳区",
                likedUsers1.size(),
                comments1.size(),
                likedUsers1,
                comments1,
                false
        ));
        
        moments.add(new Moment(
                2,
                user2,
                "发现了一家很不错的餐厅，菜品很精致，味道也很赞！推荐给大家~",
                images2,
                System.currentTimeMillis() - 172800000,
                "上海市静安区",
                likedUsers2.size(),
                comments2.size(),
                likedUsers2,
                comments2,
                false
        ));
        
        moments.add(new Moment(
                3,
                user3,
                "周末去了趟博物馆，长知识了！",
                images3,
                System.currentTimeMillis() - 259200000,
                "广州市天河区",
                likedUsers3.size(),
                0,
                likedUsers3,
                new ArrayList<>(),
                false
        ));
        
        moments.add(new Moment(
                4,
                user4,
                "新的一周，新的开始！加油！",
                new ArrayList<>(),
                System.currentTimeMillis() - 345600000,
                "",
                0,
                0,
                new ArrayList<>(),
                new ArrayList<>(),
                false
        ));
        
        mMoments.setValue(moments);
    }
}